package cn.supermarket.controller.service;

import cn.supermarket.common.annotation.Log;
import cn.supermarket.common.core.controller.BaseController;
import cn.supermarket.common.core.entity.Result;
import cn.supermarket.common.core.page.TableDataInfo;
import cn.supermarket.common.enums.BusinessType;
import cn.supermarket.common.utils.SecurityUtils;
import cn.supermarket.system.dto.TSupplierDTO;
import cn.supermarket.system.entity.TSupplier;
import cn.supermarket.system.service.TSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author yp
 * @Date 2025/3/3:09:17
 */
@RestController
@RequestMapping("/service/supplier")
public class TSupplierController extends BaseController {
    @Autowired
    private TSupplierService tSupplierService;

    @GetMapping("/list")
    public TableDataInfo list(TSupplierDTO dto)
    {
        startPage();
        List<TSupplier> list = tSupplierService.pageList(dto);
        return getDataTable(list);
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('service:supplier:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated @RequestBody TSupplier tSupplier)
    {
        tSupplier.setCreateBy(SecurityUtils.getUsername());
        tSupplier.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tSupplierService.save(tSupplier));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('service:supplier:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated @RequestBody TSupplier tSupplier)
    {
        tSupplier.setCreateTime(tSupplier.getCreateTime());
        tSupplier.setUpdateBy(SecurityUtils.getUsername());
        tSupplier.setUpdateTime(new Date());
        return toAjax(tSupplierService.updateById(tSupplier));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('service:supplier:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierManagementIds}")
    public Result remove(@PathVariable String[] supplierManagementIds)
    {
        return toAjax(tSupplierService.removeByIds(Arrays.asList(supplierManagementIds)));
    }

    /**
     * 获取列表选择框
     */
    @GetMapping("/optionSelect")
    public Result optionSelect()
    {
        List<TSupplier> posts = tSupplierService.list();
        return success(posts);
    }

    /**
     * 详细信息
     */
    @GetMapping(value = "/{supplierManagementId}")
    public Result getInfo(@PathVariable String supplierManagementId)
    {
        return success(tSupplierService.getById(supplierManagementId));
    }

    /**
     * 下拉框
     */
    @GetMapping("/getSelection")
    public Result getSelection()
    {
        return success(tSupplierService.list());
    }
}
